package com.zhuss.core.processor;

import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.login.UpdatePicBo;
import com.zhuss.bean.bo.core.upload.DeleteUploadBo;
import com.zhuss.bean.vo.core.user.DetailUserVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.UserDao;
import com.zhuss.core.db.entity.User;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.AabException;
import com.zhuss.util.JsonUtil;

/**
 * 修改头像  processor
 * @Author: zhushanshuo
 * @Date: 2020/03/10 17:30
 */
@Component("picUpdateProcessor")
public class PicUpdateProcessor extends AbstractProcessor<Object, UpdatePicBo> implements Processor<Object, UpdatePicBo> {
	
	@Autowired
    private UserDao userDao;
    
	@Autowired
	@Qualifier("userRead")
	private UserRead userRead;
	
	@Autowired
	@Qualifier("uploadDeleteProcessor")
	private Processor<Object, DeleteUploadBo> uploadDeleteProcessor;
    
    
	
    /**
     * 校验
     */
    @Override
    public void check(UpdatePicBo bo) throws AabException {
    }
    
    /**
     * 操作方法
     * @throws AabException 
     */
    @Override
    public Object process(UpdatePicBo bo) throws AabException {
    	Long userId = bo.getConcurrentUserId();
    	String pic = bo.getPic();
    	String phone = bo.getPhone();
    	
    	DetailUserVo user = (DetailUserVo) userRead.detail(userId);
    	String picDb = user.getPic();
    	
    	User record = new User();
    	record.setUserId(userId);
    	record.setPic(pic);
    	record.setPhone(phone);
    	userDao.updateByPrimaryKeySelective(record);
    	
    	if (picDb != null && !"".equals(picDb.trim()) && !picDb.equals(pic)) {
    		DeleteUploadBo deleteUploadBo = new DeleteUploadBo();
    		BeanUtils.copyProperties(bo, deleteUploadBo);
    		deleteUploadBo.setKey(picDb);
    		uploadDeleteProcessor.process(deleteUploadBo);
    	}
    	
    	
    	return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(UpdatePicBo param) throws AabException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.修改头像.key());
    	record.setTargetId(param.getConcurrentUserId());
    	record.setTargetName(param.getPic());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
